package com.yz.jdd.module.main;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.module.main.adapter.OrdersAdapter;
import com.yz.jdd.module.main.entity.FilterParams;
import com.yz.jdd.module.main.presenter.FilterOrderContract;
import com.yz.jdd.module.main.presenter.FilterOrderPresenter;
import com.yz.jdd.module.main.presenter.OrderContract;
import com.yz.jdd.module.main.presenter.OrderPresenter;


/**
 * 筛选新列表页面
 */
public class FilterOrderListActivity extends BaseActivity implements FilterOrderContract.View, ItemOnClick {


    //条件筛选
    ImageView filtrateImg;

    //原订单列表
    public SmartRefreshLayout srl_main_update;//下拉刷新
    ExpandableListView expand_list;
    LinearLayout nodataLayout;
    private OrdersAdapter orderExtendableListViewAdapter;
    private FilterOrderContract.Presenter presenter;
    private TextView new_dataHint;

    // 订单类型切换
    private TextView titleOrderType;
    private TextView top_back;

    // 收银订单列表
    private SmartRefreshLayout cashierOrderSrl_update;//下拉刷新
    private ExpandableListView cashierOrderExpand_list;

    //是否为收银筛选
    private String isCashList;

    private String bizType="";
    private String orderType = "";
    private String payType = "";
    private String orderStatus = "";
    private String orderSource = "";
    private String shopId = "";
    private String startDate = "";
    private String endDate = "";

    private String deviceId = "";
    private String operatorId = "";

    private FilterParams filterParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_order_list);
        ImmersionBar.with(this)
                .reset()  //重置所以沉浸式参数
                .transparentStatusBar()  //透明状态栏，不写默认透明色
                .statusBarColor(R.color.colorPrimary)     //状态栏颜色，不写默认透明色
                .statusBarDarkFont(false, 0.01f)   //状态栏字体是深色，不写默认为亮色
                .flymeOSStatusBarFontColor(R.color.color_333333)  //修改flyme OS状态栏字体颜色
                .fitsSystemWindows(true)    //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色
                .supportActionBar(false) //支持ActionBar使用
                .addTag("tag")  //给以上设置的参数打标记
                .getTag("tag")  //根据tag获得沉浸式参数
                .init();

        //title
        filtrateImg = findViewById(R.id.filtrate);
        titleOrderType = findViewById(R.id.title_order_type);
        top_back = findViewById(R.id.top_back);
        top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Bundle bundle = getIntent().getExtras();
        isCashList = bundle.getString("isCashList");
        orderType = bundle.getString("orderType");

        filterParams = (FilterParams) bundle.getSerializable("filterParams");
        if (filterParams != null) {
            //    orderType = filterParams.getOrderType();
            bizType=filterParams.getBizType();
            payType = filterParams.getPayType();
            orderStatus = filterParams.getOrderStatus();
            orderSource = filterParams.getOrderSource();
            shopId = filterParams.getShopId();
            startDate = filterParams.getStartTime();
            endDate = filterParams.getEndTime();

            deviceId = filterParams.getDeviceId();
            operatorId = filterParams.getOperatorId();
        }

        titleOrderType.setText(isCashList);

        //原订单列表
        new_dataHint = findViewById(R.id.new_data_hint);
        nodataLayout = findViewById(R.id.nodata_layout);
        presenter = new FilterOrderPresenter(FilterOrderListActivity.this, R.layout.item_order_list, this);
        srl_main_update = findViewById(R.id.srl_main_update);
        expand_list = findViewById(R.id.expand_list);

        //新收银订单
        cashierOrderSrl_update = findViewById(R.id.srl_cashier_order);//下拉刷新
        cashierOrderExpand_list = findViewById(R.id.cashier_order_expand_list);

        filtrateImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FilterOrderListActivity.this, FilterActivity.class);
                Bundle bundle = new Bundle();
                if ("收银订单".equals(isCashList)) {
                    intent.putExtra("isCashList", isCashList);
                } else {
                    intent.putExtra("isCashList", isCashList);
                }
                intent.putExtra("orderType", orderType);
                if (filterParams != null) {
                    bundle.putSerializable("filterParams", filterParams);
                }
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });

        srl_main_update.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                presenter.doGetOrderList(true, bizType, payType, orderStatus, orderSource, shopId, startDate, endDate, orderType, deviceId, operatorId);
            }

        });
        srl_main_update.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                presenter.doGetOrderList(false,bizType, payType, orderStatus, orderSource, shopId, startDate, endDate, orderType, deviceId, operatorId);
            }
        });

        cashierOrderSrl_update.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                presenter.doGetFilterCashierOrderList(true,bizType, payType, orderStatus, orderSource, shopId, startDate, endDate, deviceId, operatorId);
            }

        });
        cashierOrderSrl_update.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                presenter.doGetFilterCashierOrderList(false, bizType,payType, orderStatus, orderSource, shopId, startDate, endDate, deviceId, operatorId);
            }
        });
        if ("收银订单".equals(isCashList)) {
            cashierOrderSrl_update.setVisibility(View.VISIBLE);
            srl_main_update.setVisibility(View.GONE);
            presenter.doGetFilterCashierOrderList(true,bizType, payType, orderStatus, orderSource, shopId, startDate, endDate, deviceId, operatorId);
        } else {
            cashierOrderSrl_update.setVisibility(View.GONE);
            srl_main_update.setVisibility(View.VISIBLE);
            presenter.doGetOrderList(true, bizType,payType, orderStatus, orderSource, shopId, startDate, endDate, orderType, deviceId, operatorId);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void noMore() {
        if ("收银订单".equals(titleOrderType.getText())) {
            cashierOrderSrl_update.setNoMoreData(true);
            cashierOrderSrl_update.setEnableLoadMore(false);
        } else {
            srl_main_update.setNoMoreData(true);
            srl_main_update.setEnableLoadMore(false);
        }
    }

    @Override
    public void More() {
        if ("收银订单".equals(titleOrderType.getText())) {
            cashierOrderSrl_update.setEnableLoadMore(true);
            cashierOrderSrl_update.setNoMoreData(false);
        } else {
            srl_main_update.setEnableLoadMore(true);
            srl_main_update.setNoMoreData(false);
        }
    }

    @Override
    public void noData() {
        if (!"收银订单".equals(titleOrderType.getText())) {
            nodataLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void Data() {
        if (!"收银订单".equals(titleOrderType.getText())) {
            nodataLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void refreshComplete(boolean isRefresh) {
        if (!"收银订单".equals(titleOrderType.getText())) {
            srl_main_update.finishRefresh();
            srl_main_update.finishLoadMore();
        } else {
            cashierOrderSrl_update.finishRefresh();
            cashierOrderSrl_update.finishLoadMore();
        }
        if (isRefresh) {
            if (new_dataHint.getVisibility() == View.VISIBLE) {
                new_dataHint.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void setPresenter(FilterOrderPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void setAdapter() {
        orderExtendableListViewAdapter = presenter.getAdapter();
        expand_list.setAdapter(orderExtendableListViewAdapter);

        //设置分组的监听
        expand_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true; // true为不能点击收缩：
            }
        });
        //设置子项布局监听
        expand_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                presenter.startOrderDetail(groupPosition, childPosition, false, orderType);
                return true;

            }
        });


    }

    @Override
    public void setCashAdapter() {
        presenter.getCashAdapter().setItemOnClick(this);
        cashierOrderExpand_list.setAdapter(presenter.getCashAdapter());

        //设置分组的监听
        cashierOrderExpand_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false; // true为不能点击收缩：
            }
        });


    }

    @Override
    public void layoutOnClick(Boolean isMore, int groupId, int childId) {
        if (isMore) {
            //加载更多
            presenter.doGetFilterCashierOrderList(false, bizType,payType, orderStatus, orderSource, shopId, startDate, endDate, deviceId, operatorId);
        } else {
            presenter.startOrderDetail(groupId, childId, true, "0"); // 跳转至详情页面
        }
    }


    @Override
    public void expandGroup(int size) {
        int groupCount = orderExtendableListViewAdapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            expand_list.expandGroup(i);

        }
    }

    @Override
    public void expandGroup() {

        cashierOrderExpand_list.expandGroup(0);
        //设置分组的监听
        cashierOrderExpand_list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                LogUtils.i("---------------------onGroupExpand------------------------------------");
                int count = cashierOrderExpand_list.getExpandableListAdapter().getGroupCount();
                for (int j = 0; j < count; j++) {
                    if (j != groupPosition) {
                        cashierOrderExpand_list.collapseGroup(j);
                    }
                }

            }
        });
    }


}
